<?php

namespace App\Services\Admin;

use App\Models\ClassQuarterly as Quarterly;

class ClassQuarterly extends Base
{
    /**
     * @param string $is_all  查全部还是时间范围查询  1查全部  不填则查时间范围
     * @return mixed
     */
    public static function lists($is_all = '')
    {

        $input = request()->all();

        $where[] = ['is_display','1'];

        if($is_all == ''){
            $new_time = date('Y-m-d');
            $where[] = ['start_time','<=', $new_time];
            $where[] = ['end_time','>=', $new_time];
        }

        return Quarterly::where($where)->orderBy('id','desc');

    }

    public static function info($id)
    {
        return (new Quarterly)->where('id', $id)->first();
    }

    public static function add($param = [])
    {
        $param['start_time'] = "{$param['start_time']} 00:00:00";
        $param['end_time'] = "{$param['end_time']} 23:59:59";

        $where_all[] = ['start_time','>=',$param['start_time']];
        $where_all[] = ['end_time','<=',$param['end_time']];
        $where_all[] = ['is_display','=','1'];

        $where_start_time[] = ['start_time','>=',$param['start_time']];
        $where_start_time[] = ['start_time','<=',$param['end_time']];
        $where_start_time[] = ['is_display','=','1'];

        $where_end_time[] = ['end_time','>=',$param['start_time']];
        $where_end_time[] = ['end_time','<=',$param['end_time']];
        $where_end_time[] = ['is_display','=','1'];

        $quarterly = (new Quarterly)
            ->orWhere($where_start_time)
            ->orWhere($where_end_time)
            ->orWhere($where_all)
            ->get();

        if($quarterly->isNotEmpty())return ['bool'=>false,'message'=>'该时间段已设置过了'];

        (new Quarterly)->insert($param);

        return ['bool'=>true,'message'=>''];
    }

    public static function edit($id , $param=[])
    {
        return (new Quarterly)->where('id',$id)->update($param);
    }

    public static function del($id)
    {
        return (new Quarterly)->where('id',$id)->update(['is_display'=>'-1']);
    }
}
